/**
 * 北谷信息科技（广州）有限公司拥有本软件所有版权 © 2022，并保留所有权利。
 * Copyright © 2022, North Valley Information Technology (Guangzhou) Company
 * Limited, All Rights Reserved.
 */

package main.com.scrapy;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import main.com.scrapy.utils.HttpUtil;
import main.com.scrapy.utils.JsonUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

/**
 * TODO: 给出类和接口说明
 *
 * @author Double
 * <p><b>Creation Time:</b> 2022/9/19 10:26</p>
 * @since scrapy-1688
 */
public class HtmlScrapy {

  public void testScrapy() throws IOException {
    File file = new File("D:/ideaproject/input.xlsx");
    System.out.println("文件路径" + ScrapyMain.class.getResource("").getPath());
    System.out.println("文件路径" + file.getAbsolutePath());
    System.out.println("文件路径" + System.getProperty("user.dir"));

    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet(HttpUtil.COMPANY_URL);
    httpGet.setHeader("User-Agent",
        "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36");
    CloseableHttpResponse response = httpClient.execute(httpGet);
    String responseStr = EntityUtils.toString(response.getEntity(), "utf-8");

    Document doc = Jsoup.parse(new URL(HttpUtil.PRODUCT_URL), 3000);

    String uriRegex = "\"domainUri\":\".*?.1688.com\"";
    for (String uri : JsonUtil.getJsonResult(doc.data(), uriRegex)) {
      String uriStr = uri.split(":")[1];
      //      uriList.add(uriStr.substring(1, uriStr.length() - 1));
    }

    //    for (String uri : uriList) {
    //      String uri1 = HttpUtil.HTTP_PREFIX + uri;
    //
    //      Document doc = HttpUtil.getHttpDocument(uri1);
    //      Element dev = doc.getElementById("content");
    //      String attr = dev.attr("data-user-context");
    //      JSONObject jsonObject = JSON.parseObject(attr);
    //      Object uid = jsonObject.get("uid");
    //      Map<String, Object> map = new HashMap<>();
    //      map.put("memberId", uid);
    //    }

    // 解析script内容
    Elements elements = doc.getElementsByTag("script");
    elements.forEach(e -> {
      String data1 = e.data();
      if (data1.contains("window.data.offerresultData")) {
        int start = data1.indexOf("window.data.offerresultData");
        int end = data1.indexOf(");", start);
        String substring = data1.substring(start, end + 1);

        System.out.println(substring);
      }
    });
  }
}
